window.addEvent('domready',function(){
	$('searchForm').addEventListener('submit',function(e){
		e.preventDefault();
		var url = this.url.value;
		var reg = /^(http(s)?:\/\/)?([a-z0-9]([\w-]*\.))+([a-z]+)+(:([0-9]+))?(\/(((([a-z0-9$.+_-]|[*()\'!,%])|([0-9a-fA-F]){2})|[;:@&=~])*(\/((([a-z0-9$.+_-]|[*()\'!,%])|([0-9a-fA-F]){2})|[\;:@&=~])*)*)([?]((([a-z0-9$.+_-]|[*()\'!,%])|([0-9a-fA-F]){2})|[\;:@&=~])*)?)?$/i;
		if(reg.test(url)){
			cc.crawl(url);
		}else{
			alert('url is error');
		}
	});
	cc.nav.initialize();
});

var cc = {}
cc.crawl = function(url){
	$('content').set('html','<img src="/images/loading.gif" alt="loading..."/>');
	new Request({
		url:'/crawl',
		method:'get',
		data:'url='+url,
		onSuccess: function(html, xml){
			if(html == 'false'){
				$("content").set('html','Ohh, Error Occues, Please repeat!');
			}else{
				$("content").set("html","<iframe src='http://192.168.243.128/www/skipfish/"+html+"'></iframe");
			}
			console.info('success'+html);
		},
		onFailure: function(xhr){
			console.info('error');
		}
	}).send();
}
cc.nav = function(){
	var elements = {};
	var values = {
		searchHeight:	null,
		selected:		'selected',
		category:		'category',
		open:			'open',
		catSelected:	'cat-selected',
		sub:			'sub',
		hasfocus:		true,
		loader:			'<div id="loader"></div>',
		title:			'skipfish for dianping'
	};

	var keys = {
		enter:	13,
		escape:	27,
		up:		38,
		down:	40,
		array:	[13,27,38,40]
	}

	function initialize(){
		elements = {
			search:	$('search-filed'),
			searchWrappper:	$('search'),
			content:	$('content'),
			list:		$('static-list'),
			window:		$(window),
			results:	null,
			category:	$$('.category')
		};

		elements.results = '';
//		elements.category = null,
		elements.window.addEvents({
			'resize': function(){
//				console.info('resize');
			},
			'mouseover':function(){
//				console.info('mouseover');

			},
			'keydown':function(){
//				console.info('keydown');
			}
		});

		//lists
//		$$('.' + values.category).getElements('span').addEvent('click', function(){
//			console.info('click');
//		});

		elements.category.getElement('span').toggle('click', function(){
			clearSelected();
			$(this).getParent().addClass(values.open).getElement('ul').setStyle('display','block');
		},function(){
			clearSelected();
			$(this).getParent().removeClass(values.open).getElement('ul').setStyle('display','none');
		});

		elements.list.getElements('a').addEvent('click',function(e){
			e.preventDefault();
			$('content').set('html', '<iframe src="' + this.href + '"></iframe>');			
			console.info(this.href);			
		})

	}

	Element.implement({
		toggle:function(event, fn1, fn2){
			var flag = true;
			return this.addEvent(event, function(){
				(flag ? fn1 : fn2).apply(this, arguments);
				flag = !flag;
			})
		}	
	});
	function clearSelected(){
		$$('.'+values.selected).removeClass(values.selected);
	}
	return {
		initialize: initialize
	}
}();
